package com.xixibaby.core.message;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;

@Setter
@Getter
public class Delay implements Serializable {
    private Date date;
    private Integer delay;
    public Delay(){}
    public Delay(Date date,Integer delay){
        this.date = date;
        this.delay = delay;
    }

    /**
     * 获取基于当前时间的延时秒数
     * @return
     */
    public Integer getDelayAsNow(){
       Long time =  this.date.getTime() + delay * 1000 - System.currentTimeMillis();
       Long ms = time / 1000;
       return ms.intValue();
    }

    public Date getResultDate(){
        Long time = this.date.getTime() + delay * 1000;
        return new Date(time);
    }

    public static void main(String[] args) {
        DateTime parse = DateUtil.parse("2021-01-22 22:50:00", NORM_DATETIME_FORMAT);
        Delay delay = new Delay(parse,61);

        System.out.println(delay.getDelayAsNow());
        System.out.println(DateUtil.format(delay.getResultDate(),NORM_DATETIME_FORMAT));

    }
}
